﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InstituteManager.Data;
using System.Data;

namespace InstituteManager.Bussiness
{
    public class PostingBL
    {
        public static string errorMsg = "";
        public static List<tbl_Posting> getByAll()
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            List<tbl_Posting> exhi = InsMana.tbl_Posting.ToList<tbl_Posting>();
            return exhi;
        }

        public static tbl_Posting getById(int Id)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            return InsMana.tbl_Posting.Include("tbl_Competition").Include("tbl_Student").First(temp => temp.PostingId == Id);
        }

        public static bool AddNew(tbl_Posting ex, int CompetitionId, string Username)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                if (CompetitionId > 0 )
                    ex.tbl_Competition = InsMana.tbl_Competition.First(temp => temp.CompetitionId == CompetitionId);
                try
                {
                    ex.tbl_Student = InsMana.tbl_Student.First(temp => temp.Username == Username);
                }
                catch (Exception)
                {
                    errorMsg = "You don't have permission to make a Posting ! Only Student can do that.";
                    return false;
                }
                InsMana.AddTotbl_Posting(ex);

                InsMana.SaveChanges();
            }
            catch (Exception e)
            {
                errorMsg = e.Message;
                return false;
            }
            errorMsg = "";
            return true;
        }

        public static bool Update(tbl_Posting ex, int CompetitionId)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                tbl_Posting obj = InsMana.tbl_Posting.First(temp => temp.PostingId == ex.PostingId);
                obj.PostingName = ex.PostingName;
                obj.PostDate = ex.PostDate;
                obj.Description = ex.Description;
                obj.Price = ex.Price;
                if (ex.ImageUrl != null ) 
                    obj.ImageUrl = ex.ImageUrl;
                if (CompetitionId > 0)
                    obj.tbl_Competition = InsMana.tbl_Competition.First(temp => temp.CompetitionId == CompetitionId);
                else
                    obj.tbl_Competition = null;

                InsMana.SaveChanges();
            }
            catch (Exception e)
            {
                errorMsg = e.Message;
                return false;
            }
            errorMsg = "";
            return true;
        }

        public static bool Delete(int Id)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                InsMana.DeleteObject(InsMana.tbl_Posting.First(temp => temp.PostingId == Id));
                InsMana.SaveChanges();
            }
            catch (Exception e)
            {
                errorMsg = e.Message;
                return false;
            }
            errorMsg = "";
            return true;
        }


        public static List<tbl_Posting> SearchByName(string PostingName)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();

            return (from sc in InsMana.tbl_Posting where sc.PostingName.Contains(PostingName) select sc).ToList<tbl_Posting>();
        }

        public static bool TeacherMark(int CompetitionId,int PostingId,int Mark, string Remark)
        {
            try
            {
                InstituteManagerEntities InsMana = new InstituteManagerEntities();
                Data.tbl_Posting obj = InsMana.tbl_Posting.First(temp => temp.PostingId == PostingId);
                obj.Mark = Mark;
                obj.Remark = Remark;
                // If Mark is Best then reupdate all other Best posting of Competition to Better
                if (Mark == 1)
                {
                    foreach (tbl_Posting post in (from sc in InsMana.tbl_Posting.Include("tbl_Competition") where sc.Mark == 1 && sc.tbl_Competition.CompetitionId == CompetitionId && sc.PostingId != PostingId select sc))
                    {
                        post.Mark++;
                    }
                }
                InsMana.SaveChanges();
            }
            catch (Exception e)
            {
                errorMsg = e.Message;
                return false;
            }
            return true;
        }

        public static bool isExhibibionSent(int PostingId)
        {
            InstituteManagerEntities InsMana = new InstituteManagerEntities();
            if ((from sc in InsMana.tbl_ExhibitionDetail.Include("tbl_Exhibition").Include("tbl_Posting") where sc.tbl_Posting.PostingId == PostingId select sc).Count() < 1)
                return false;
            return true;
        }
    }
}
